package Interfaces;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

import Modelo.Modelo;


public class InterfacePrograma extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextArea visor;
	private Border borderVisor;
	private JButton novaFormula, alteraFormula;
	
	private JButton[] f;
	
	private Modelo _modelo;
	
	private int selected = -1;
	
	public InterfacePrograma(Modelo modelo, JButton[] buttons)
	{
		f = buttons;
		
		_modelo = modelo;
		_modelo.setInterfacePrograma(this);
		Dimension d = _modelo.getInterfacePrincipal().getSize();
		setSize(d);
		setPreferredSize(d);
		setMinimumSize(d);
		setMaximumSize(d);
		InicializeComponents();
		
		addComponents();
		
		addListeners();
		
	}

	private void addComponents()
	{
		this.add(visor);
		visor.setSize(310, 100);
		visor.setPreferredSize(new Dimension(310, 100));
		visor.setMaximumSize(new Dimension(310, 100));
		visor.setMinimumSize(new Dimension(310, 100));

		Dimension dd = new Dimension((int)getSize().getWidth()/2-23, 30);
		this.add(novaFormula);
		novaFormula.setSize(dd);
		novaFormula.setPreferredSize(dd);
		novaFormula.setMaximumSize(dd);
		novaFormula.setMinimumSize(dd);
		
		this.add(alteraFormula);
		alteraFormula.setSize(dd);
		alteraFormula.setPreferredSize(dd);
		alteraFormula.setMaximumSize(dd);
		alteraFormula.setMinimumSize(dd);
		
		for(int i = 0; i < 18; i++)
		{
			f[i].addActionListener(new SeleccionaListener(visor, _modelo, i));
			
			this.add(f[i]);
		}
		
	}
	
	private class SeleccionaListener implements ActionListener
	{
		int index;
		JTextArea text;
		Modelo modelo;
		
		public SeleccionaListener(JTextArea text, Modelo modelo, int index)
		{
			this.text = text;
			this.index = index;
			this.modelo = modelo;
		}
		
		@Override
		public void actionPerformed(ActionEvent e)
		{
			selected = index;
			
			text.setText("\n\t" + modelo.getEqNome(index) + ": \n\t\t" + modelo.getFormula(index));
		
			text.repaint();
		}
	}
	
	private void InicializeComponents() {
		
		visor=new JTextArea("\n\tPreview da Fórmula...", 5, 42);
		borderVisor=BorderFactory.createLineBorder(Color.BLACK);
		visor.setBorder(borderVisor);

		novaFormula=new JButton("Nova Fórmula");
		alteraFormula=new JButton("Alterar Fórmula");
		
	}
	
	public JTextArea getVisor()
	{
		return visor;
	}
	

	private void addListeners(){
		novaFormula.addActionListener(new novaFormulaListener(this));
		alteraFormula.addActionListener(new alteraFormulaListener(this));
		//*****************************************************************
	}
	

	public class novaFormulaListener implements ActionListener
	{
		InterfacePrograma it;
		
		novaFormulaListener(InterfacePrograma it)
		{
			this.it = it;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			if(selected == -1)
				JOptionPane.showMessageDialog(it, "Tem que seleccionar um botão primeiro.", "Informação", JOptionPane.QUESTION_MESSAGE);
			else
				new InsereAlteraFormula(_modelo, selected, true);//*****************************************
		}
    }
	public class alteraFormulaListener implements ActionListener
	{
		InterfacePrograma it;
		
		alteraFormulaListener(InterfacePrograma it)
		{
			this.it = it;
		}
		
		@Override
		public void actionPerformed(ActionEvent e)
		{
			if(selected == -1)
				JOptionPane.showMessageDialog(it, "Tem que seleccionar um botão primeiro.", "Informação", JOptionPane.QUESTION_MESSAGE);
			else
			{
				if(_modelo.isVazio(selected))
					JOptionPane.showMessageDialog(it, "Tem que adicionar uma fórmula primeiro.", "Informação", JOptionPane.QUESTION_MESSAGE);
				else
					new InsereAlteraFormula(_modelo,selected, false);//*********************************************
				
			}
		}
    }
	
}
